<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | CMS设置
// +----------------------------------------------------------------------
namespace app\cms\controller;

use app\admin\model\Module as Module_Model;
use app\common\controller\Adminbase;
use think\facade\Cache;
use think\facade\Session;
use think\Db;

class Setting extends Adminbase
{
    //cms设置
    public function index()
    {
        if ($this->request->isPost()) {
            $setting         = $this->request->param('setting/a');
            $data['setting'] = serialize($setting);
            if (Module_Model::update($data, ['module' => 'cms'])) {
                cache('Cms_Config', null);
                $this->success("更新成功！");
            } else {
                $this->success("更新失败！");
            }
        } else {
            $setting = Module_Model::where('module', 'cms')->value("setting");
            $this->assign("setting", unserialize($setting));
            $this->assign("current_language", $current_language);
            return $this->fetch();
        }
    }

    public function changeLanguage(){
        $param = $this->request->param();
        Session::set('language',$param['language']);
        Cache::clear();
        $this->success("切换成功！");
    }

    public function transform(){
        $param = $this->request->param();
        $language = $param['language'];
        $count = Db::name('category')->where('language',$language)->count();
        if($count > 0){
            $this->error("已存在数据！");
        }
        // 栏目
        $categoryList = Db::name('category')->where('language','default')->select();
        $maxId = Db::name('category')->max('id');
        foreach($categoryList as $key => $value){
            $categoryList[$key]['id'] = $value['id'] + $maxId;
            $categoryList[$key]['language'] = $language;
            $categoryList[$key]['catdir'] = $value['catdir'].'2';
            $categoryList[$key]['parentid'] = $this->getNewCategory($value['parentid'], $maxId);
            $categoryList[$key]['arrparentid'] = $this->getNewCategory($value['arrparentid'],$maxId);
            $categoryList[$key]['arrchildid'] = $this->getNewCategory($value['arrchildid'],$maxId);
        }
        // 模型
        $model = Db::name('model')->where('module','cms')->select();
        foreach($model as $key => $value){
            $list[$value['tablename']] = Db::name($value['tablename'])->where('language','default')->select();
            $modelMaxId = Db::name($value['tablename'])->max('id');
            foreach($list[$value['tablename']] as $k => $v){
                $list[$value['tablename']][$k]['id'] = $v['id'] + $modelMaxId;
                $list[$value['tablename']][$k]['language'] = $language;
                $list[$value['tablename']][$k]['catid'] = $v['catid'] + $maxId;
            }
            if($value['type'] == 2){
                $list[$value['tablename'].'_data'] = Db::name($value['tablename'].'_data')->select();
                foreach($list[$value['tablename'].'_data'] as $k => $v){
                    $list[$value['tablename'].'_data'][$k]['did'] = $v['did'] + $modelMaxId;
                }
            }
        }
        // 单页
        $pageList = Db::name('page')->where('language','default')->select();
        foreach($pageList as $key => $value){
            $pageList[$key]['catid'] = $value['catid'] + $maxId;
            $pageList[$key]['language'] = $language;
        }
        Db::startTrans();
        try {
            Db::name('category')->insertAll($categoryList);
            foreach($list as $key => $value){
                Db::name($key)->insertAll($value);
            }
            Db::name('page')->insertAll($pageList);
            $res = true;
            Db::commit();
        } catch (\Exception $e) {
            $res = false;
            Db::rollback();
        }
        $res ? $this->success("转换成功！") : $this->error("转换成功！");
    }

    public function getNewCategory($string, $maxId){
        $arr = explode(',',$string);
        foreach($arr as $key => $value){
            if($value == 0){
                continue;
            }
            $arr[$key] = intval($value) + $maxId;
        }
        return implode(',',$arr);
    }

}
